import allure
from appium.webdriver.common.appiumby import AppiumBy

from utils.log_util import logger

black_list = [
    (AppiumBy.XPATH, "//*[text='取消']"),
    (AppiumBy.XPATH, "//*[text='关闭']"),
]


# 装饰器，黑名单
def black(func):
    def inner(*args, **kwargs):
        basepage = args[0]
        basepage.driver.implicitly_wait(1)
        try:
            logger.info(f'开始查找元素{args}')
            basepage.driver.implicitly_wait(15)
            return func(*args, **kwargs)
        except Exception as e:
            logger.info(f'没有找到元素,出来异常{e}')
            image_path = basepage.screen()
            allure.attach.file(
                image_path,
                name='异常捕捉截图',
                attachment_type=allure.attachment_type.PNG
            )
            pagesource_path = basepage.source()
            allure.attach.file(
                pagesource_path,
                name='捕捉异常源码',
                attachment_type=allure.attachment_type.TEXT
            )
            for b in black_list:
                eles = basepage.driver.find_elements(*b)
                if len(eles) > 0:
                    eles = basepage.driver.find_elements(*b)[0].click()
                    basepage.driver.implicitly_wait(15)
                    return func(*args, **kwargs)
            logger.info(f'遍历黑名单未找到元素')
            raise e
    return inner
